import vue from "@vitejs/plugin-vue";
import { viteMockServe } from "vite-plugin-mock";
import { resolve } from "path";

const pathResolve = dir => {
  return resolve(__dirname, ".", dir);
};

const alias = {
  "@": pathResolve("src")
};

// https://vitejs.dev/config/
export default ({ command }) => {
  const prodMock = true;
  return {
    base: "./",
    resolve: {
      dedupe: ["vue"],
      alias: {
        "@": "/src"
      }
    },
    define: {
      __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: true,
      __VUE_I18N_FULL_INSTALL__: true,
      __VUE_I18N_LEGACY_API__: true,
      __INTLIFY_PROD_DEVTOOLS__: false
    },
    server: {
      port: 8080,
      host: "0.0.0.0",
      open: true,
      proxy: {
        // 代理配置
        "/dev":
          "https://www.fastmock.site/mock/48cab8545e64d93ff9ba66a87ad04f6b/"
      }
    },
    build: {
      // outDir: "./dxt",
      chunkSizeWarningLimit: 5000,
      rollupOptions: {
        output: {
          // 最小化拆分包
          // manualChunks(id) {
          //   if (id.includes("node_modules")) {
          //     return id
          //       .toString()
          //       .split("node_modules/")[1]
          //       .split("/")[0]
          //       .toString();
          //   }
          // },
          // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
          // entryFileNames: "js/[name].[hash].js",
          // 用于命名代码拆分时创建的共享块的输出命名
          // 　　chunkFileNames: 'js/[name].[hash].js',
          // 用于输出静态资源的命名，[ext]表示文件扩展名
          // assetFileNames: "[ext]/[name].[hash].[ext]"
          // 拆分js到模块文件夹
          // chunkFileNames: chunkInfo => {
          //   const facadeModuleId = chunkInfo.facadeModuleId
          //     ? chunkInfo.facadeModuleId.split("/")
          //     : [];
          //   const fileName =
          //     facadeModuleId[facadeModuleId.length - 2] || "[name]";
          //   return `js/${fileName}/[name].[hash].js`;
          // }
        }
      }
    },
    plugins: [
      vue(),
      // Components({
      // dirs: ['src/components'] // 配置需要默认导入的自定义组件文件夹，该文件夹下的所有组件都会自动 import
      // resolvers: [AntDesignVueResolver({ importStyle: false, resolveIcons: true })]
      // }),
      // Components({ /* options */ }),
      viteMockServe({
        mockPath: "mock",
        localEnabled: command === "serve",
        prodEnabled: command !== "serve" && prodMock,
        watchFiles: true,
        injectCode: `
          import { setupProdMockServer } from '../mockProdServer';
          setupProdMockServer();
        `,
        logger: true
      })
    ]
  };
};
